// SPDX-FileCopyrightText: 2023-2025 Sangfor Technologies Inc.
// SPDX-License-Identifier: Mulan PSL v2
package com.geniusai.aip.auth.utils;

import java.text.DecimalFormat;
import java.util.regex.Pattern;

/**
 * 把其它单位统一转换为MB
 *
 * @author: 37680 xmz
 * @Date: 2022/7/9 10:34
 */
public class CovnerUnitUtils {

    public static final String SIZETYPE_B = "B";
    public static final String SIZETYPE_KB = "KB";
    public static final String SIZETYPE_MB = "MB";
    public static final String SIZETYPE_GB = "GB";
    public static final String SIZETYPE_TB = "TB";
    public static final String SIZETYPE_PB = "PB";
    public static final Pattern zeroDecimalPattern = Pattern.compile("\\d+\\.0+");

    /**
     * 传入TB,GB,PB,MB，转换为MB
     *
     * @param disk 传入参数
     * @return 返回MB
     */
    public static double covnerMB(String disk){
        DecimalFormat df = new DecimalFormat("#.00");
        String sizeType = disk.substring(disk.length() -2);
        double size = Double.parseDouble(disk.substring(0, disk.length() - 2));
        double resSize = 0;

        switch (sizeType) {
            case SIZETYPE_MB:
                resSize = Double.parseDouble(df.format(size));
                break;
            case SIZETYPE_GB:
                resSize = Double.parseDouble(df.format(1.0 * size * 1024));
                break;
            case SIZETYPE_TB:
                resSize = Double.parseDouble(df.format(size * 1024 * 1024));
                break;
            case SIZETYPE_PB:
                resSize = Double.parseDouble(df.format(size * 1024 * 1024 * 1024));
                break;
            default:
                break;
        }
        return resSize;
    }

    /**
     * 传入MB,自动转换合适大小
     */
    public static String formatSize(double size) {
        DecimalFormat df = new DecimalFormat("#.00");
        String fileSizeString = "";
        String wrongSize = "0MB";
        if (size == 0) {
            return wrongSize;
        }
        if (size < 1024) {
            fileSizeString = df.format( size) + SIZETYPE_MB;
        } else if (size < 1048576) {
            fileSizeString = df.format( size / 1024) + SIZETYPE_GB;
        } else if (size < 1073741824) {
            fileSizeString = df.format( size / 1048576) + SIZETYPE_TB;
        } else if (size < 1073741824L * 1024){
            fileSizeString = df.format( size / (1073741824L * 1024)) + SIZETYPE_PB;
        }
        return fileSizeString;
    }


    /**
     * 传入MB,转换文件大小,指定转换的类型
     */
    public static double formatSizeWithUnit(long size, String sizeType) {
        DecimalFormat df = new DecimalFormat("#.00");
        double fileSizeLong = 0;
        switch (sizeType) {
            case SIZETYPE_MB:
                fileSizeLong = Double.parseDouble(df.format((double) size));
                break;
            case SIZETYPE_GB:
                fileSizeLong = Double.parseDouble(df.format((double) size / 1024));
                break;
            case SIZETYPE_TB:
                fileSizeLong = Double.parseDouble(df.format((double) size / 1048576));
                break;
            case SIZETYPE_PB:
                fileSizeLong = Double.parseDouble(df.format((double) size / 1073741824));
                break;
            default:
                break;
        }
        return fileSizeLong;
    }

    /**
     * 如果小数位全为0.则只保留整数部分
     *
     */
    public static String removeZeroDecimal(double value){
        String valueStr = String.valueOf(value);
        if (zeroDecimalPattern.matcher(valueStr).matches()) {
            return valueStr.replaceAll("\\.0+", "");
        }
        return valueStr;
    }

}
